$(function () {
    $("#jqGrid").jqGrid({
        url: '../databranchcheck/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', width: 50, key: true ,hidden:true},
			{ label: '', name: 'employeeid', width: 80,hidden:true }, 			
			{ label: '工作编号', name: 'workno', width: 80 }, 			
			{ label: '岗位编号', name: 'jobno', width: 80 }, 			
			{ label: '姓名', name: 'name', width: 80 }, 			
			{ label: '任职时间(月)', name: 'jobtime', width: 80, formatter: function(value, options, row){

				if(Number(value)>=3){
					return "<span style='color:#16da16'>"+value+"</span>";
				}else if(Number(value)<3){
					return "<span style='color:red'>"+value+"</span>";
				} else {
					return value;
				}
			} }, 			
			{ label: '直接推荐', name: 'directrec', width: 80 , formatter: function(value, options, row){

				if(Number(value)>=2){
					return "<span style='color:#16da16'>"+value+"</span>";
				}else if(Number(value)<2){
					return "<span style='color:red'>"+value+"</span>";
				} else {
					return value;
				}
			}  }, 			
			{ label: '正式置业顾问推荐', name: 'normalrec', width: 80 , formatter: function(value, options, row){

				if(Number(value)>=2){
					return "<span style='color:#16da16'>"+value+"</span>";
				}else if(Number(value)<2){
					return "<span style='color:red'>"+value+"</span>";
				} else {
					return value;
				}
			}  }, 			
			{ label: '直辖组总人数（总推荐人数）', name: 'totalmember', width: 80 , formatter: function(value, options, row){
				if(row.jobno=="09"||row.jobno=="10"){
					if(Number(value)>=3){
						return "<span style='color:#16da16'>"+value+"</span>";
					}else if(Number(value)<3){
						return "<span style='color:red'>"+value+"</span>";
					} else {
						return value;
					}
				}else{
					
					if(Number(value)>=2){
						return "<span style='color:#16da16'>"+value+"</span>";
					}else if(Number(value)<2){
						return "<span style='color:red'>"+value+"</span>";
					} else {
						return value;
					}					
				}	
			} }, 			
			{ label: '分行平均(元)', name: 'groupaverage', width: 80, formatter: function(value, options, row){

				if(Number(value)>=12500){
					return "<span style='color:#16da16'>"+formatCurrency(value)+"</span>";
				}else if(Number(value)<10000){
					return "<span style='color:red'>"+formatCurrency(value)+"</span>";
				} else {
					return formatCurrency(value);
				}
			}  }, 			
			{ label: '直接育成主管', name: 'directbredzg', width: 80 , formatter: function(value, options, row){

				if(Number(value)>=1){
					return "<span style='color:#16da16'>"+value+"</span>";
				}else if(Number(value)<1){
					return "<span style='color:red'>"+value+"</span>";
				} else {
					return value;
				}
			} }, 			
			{ label: '总育成主管', name: 'totalbredzg', width: 80, formatter: function(value, options, row){

				if(Number(value)>=2){
					return "<span style='color:#16da16'>"+value+"</span>";
				}else if(Number(value)<2){
					return "<span style='color:red'>"+value+"</span>";
				} else {
					return value;
				}
			}  }, 			
			{ label: '直接育成分行', name: 'directbredfh', width: 80 , formatter: function(value, options, row){
				if(row.jobno=="09"||row.jobno=="10"){
					if(Number(value)>=1){
						return "<span style='color:#16da16'>"+value+"</span>";
					}else if(Number(value)<1){
						return "<span style='color:red'>"+value+"</span>";
					} else {
						return value;
					}
				}else{					
						return value;										
				}	
			} }, 			
			{ label: '总育成分行', name: 'totalbredfh', width: 80, formatter: function(value, options, row){
				if(row.jobno=="09"){
					if(Number(value)>=1){
						return "<span style='color:#16da16'>"+value+"</span>";
					}else if(Number(value)<1){
						return "<span style='color:red'>"+value+"</span>";
					} else {
						return value;
					}
				}else if(row.jobno=="10"){
					
					if(Number(value)>=2){
						return "<span style='color:#16da16'>"+value+"</span>";
					}else if(Number(value)<2){
						return "<span style='color:red'>"+value+"</span>";
					} else {
						return value;
					}					
				}else{
					return value;
				}	
			}   }, 			
			{ label: '计算时间', name: 'caltime', width: 80 }, 			
			{ label: '', name: 'createtime', width: 80,hidden:true }, 			
			{ label: '', name: 'createby', width: 80,hidden:true }			
        ],
		viewrecords: true,
		 height: $(window).height()-155,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$.ajax({
				url:'../datapayroll/date',
				success:function(data){
					vm.month=data.payrollmonth
				}
			})
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		value:null,
		calTime:null,
		month:"",
		title: null,
		dataBranchcheck: {}
	},
	methods: {
		query: function () {
			vm.calTime=$("#calTime").val();
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dataBranchcheck = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.dataBranchcheck.id == null ? "../databranchcheck/save" : "../databranchcheck/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.dataBranchcheck),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../databranchcheck/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../databranchcheck/info/"+id, function(r){
                vm.dataBranchcheck = r.dataBranchcheck;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'value': vm.value,"calTime":vm.calTime},
                page:page
            }).trigger("reloadGrid");
		}
	}
});
function formatCurrency(num) 
{
	if(num)
	{
		//将num中的$,去掉，将num变成一个纯粹的数据格式字符串
		num = num.toString().replace(/\$|\,/g,'');
		//如果num不是数字，则将num置0，并返回
		if(''==num || isNaN(num)){return 'Not a Number ! ';}
		//如果num是负数，则获取她的符号
		var sign = num.indexOf("-")> 0 ? '-' : '';
		//如果存在小数点，则获取数字的小数部分
		var cents = num.indexOf(".")> 0 ? num.substr(num.indexOf(".")) : '';
		cents = cents.length>1 ? cents : '' ;//注意：这里如果是使用change方法不断的调用，小数是输入不了的
		//获取数字的整数数部分
		num = num.indexOf(".")>0 ? num.substring(0,(num.indexOf("."))) : num ;
		//如果没有小数点，整数部分不能以0开头
		if('' == cents){ if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//如果有小数点，且整数的部分的长度大于1，则整数部分不能以0开头
		else{if(num.length>1 && '0' == num.substr(0,1)){return 'Not a Number ! ';}}
		//针对整数部分进行格式化处理，这是此方法的核心，也是稍难理解的一个地方，逆向的来思考或者采用简单的事例来实现就容易多了
		/*
               也可以这样想象，现在有一串数字字符串在你面前，如果让你给他家千分位的逗号的话，你是怎么来思考和操作的?
               字符串长度为0/1/2/3时都不用添加
               字符串长度大于3的时候，从右往左数，有三位字符就加一个逗号，然后继续往前数，直到不到往前数少于三位字符为止
		 */
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		{
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		}
		//将数据（符号、整数部分、小数部分）整体组合返回
		return (sign + num + cents);    
	}

}